package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.Role;
import com.baike.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author x_FG
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/role")
@Api(value = "运营角色管理接口")
@RefreshScope
public class RoleController {

    @Autowired
    private IRoleService iRoleService;

    @ApiOperation("运营角色查询方法")
    @GetMapping
    public Result getRoleInfo(){
        List<Role> list = iRoleService.list();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    @ApiOperation("角色权限增加方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role",value = "role实体类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "menuIds",value = "menu主键Id",required = true,paramType = "path")
    })
    @PostMapping("xx/{menuIds}")
    public Result insertRoleInfo(@RequestBody Role role,@PathVariable("menuIds")Integer[] menuIds){
        boolean b = iRoleService.insertRoleInfo(role,menuIds);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation("角色权限Id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "role主键id",required = true,paramType = "path")
    })
    @GetMapping("x{id}")
    public Result getRoleById(@PathVariable("id")Integer id){
        Role role = iRoleService.getRoleById(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(role);
    }

    @ApiOperation("角色权限修改方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role",value = "role实体类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "menuIds",value = "menu主键Id",required = true,paramType = "path")
    })
    @PutMapping("m/{menuIds}")
    public Result updateRoleInfo(@RequestBody Role role,@PathVariable("menuIds")Integer[] menuIds){
        boolean b = iRoleService.updateRoleInfo(role,menuIds);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation("角色权限删除方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "role实体类主键Id",required = true,paramType = "path")
    })
    @DeleteMapping("/{id}")
    public Result deleteRoleMenu(@PathVariable("id")Integer id){
        boolean b = iRoleService.deleteRoleMenu(id);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }
}
